package com.work.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.work.app.bean.ManageMenu;
import com.work.app.bean.User;
import com.work.app.bean.UserMessage;
import com.work.app.service.ManageMenuService;
import com.work.app.service.UserMessageService;
import com.work.app.service.UserService;
import com.work.app.tool.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**

 * @author hjj
 * @date 2023/10/16 18:56
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/users")
public class UserController {
    @Autowired
    private HttpServletRequest request;
    @Value("${web.upload-path}")
    private String filePath;//文件存储位置
    @Value("${web.assess-path}")
    private String assessPath;//返回给前端的头路径
    private ManageMenuService manageMenuService;
    private UserMessageService userMessageService;
    private UserService userService;

    public UserController(ManageMenuService manageMenuService, UserMessageService userMessageService, UserService userService) {
        this.manageMenuService = manageMenuService;
        this.userMessageService = userMessageService;
        this.userService = userService;
    }
    @ApiOperation("获取菜单")
    @GetMapping("/menus")
    public List<ManageMenu> getMenus(
            @ApiParam("启用状态（1业务端启用，2管理端启用，3都启用，0不启用）")@RequestParam("state")Integer state
    ){
        List<ManageMenu> list = manageMenuService.getMenusByState(state);
        for (ManageMenu menu : list){
            List<ManageMenu> subMenus = manageMenuService.getSubMenusById(menu.getId(),state);
            menu.setSubmenu(subMenus.size()==0?new ArrayList<>():subMenus);
        }
        return list;
    }

    @ApiOperation("登录并获取用户信息")
    @GetMapping
    public R<UserMessage> getUserMessage(
            @ApiParam("账号")@RequestParam("username")String username,
            @ApiParam("密码")@RequestParam("password")String password
    ){
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername,username));
        if(user == null){
            return R.err("账号不存在");
        } else if (!user.getPassword().equals(password)) {
            return R.err("密码错误");
        } else if(user.getState()!=1){
            return R.err("账号状态异常，请稍后再试");
        }else {
            UserMessage us = userMessageService.getOne(new LambdaQueryWrapper<UserMessage>().eq(UserMessage::getUserId,user.getId()));
            if(us==null){
                return R.err("系统繁忙，请稍后再试");
            }else{
                request.setAttribute("userId",user.getId());//存入userId
                us.setImageUrl(assessPath+user.getId()+us.getImageUrl());
                return R.success(us);
            }
        }

    }

}
